﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using QFramework;

namespace NovelL
{
    public class RecordManager : MonoSingleton<RecordManager>
    {
        public static int recordNum;

        private static readonly string SETTING = "setting";

        public static SettingRecord curSetting;

        [SerializeField] static float defaultMsc = 0.5f;
        [SerializeField] static float defaultVoice = 0.5f;
        [SerializeField] static float defaultSped = 4f;

        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }

        public void SaveSetting()
        {
            SettingRecord setting = new SettingRecord();
            setting = curSetting;
            string json = JsonUtility.ToJson(setting);
            try
            {
                PlayerPrefs.SetString(SETTING, json);
            }
            catch(System.Exception err)
            {
                Debug.Log("存储游戏设置异常:" + err);
            }

        }
        public void ReadSetting()
        {
            if(!PlayerPrefs.HasKey(SETTING))
            {
                curSetting.achievements = null;
                curSetting.mscVolume = defaultMsc;
                curSetting.voiceVolume = defaultVoice;
                curSetting.textSped = defaultSped;
            }
            SettingRecord setting = new SettingRecord();
            setting = JsonUtility.FromJson<SettingRecord>(PlayerPrefs.GetString(SETTING));
            curSetting.mscVolume = setting.mscVolume;
            curSetting.voiceVolume = setting.voiceVolume;
            curSetting.textSped = setting.textSped;
            curSetting.achievements = setting.achievements;
        }

        public void SaveRecord(int index, Record record)
        {
            Record saveRecord = new Record();
            saveRecord = record;
            string json = JsonUtility.ToJson(saveRecord);
            try
            {
                PlayerPrefs.SetString(index.ToString(), json);
            }
            catch(System.Exception err)
            {
                Debug.Log("Got savegameerror:" + err);
            }
        }
        

    }

    [System.Serializable]
    public class Record
    {
        public int scriptIndex; //脚本进行到第几行
        public string imgPath;  //图片上下文
        public string mscPath;  //背景音乐上下文

        public bool isEmpty; //空存档
        
    }

    [System.Serializable]
    public class SettingRecord
    {
        public float mscVolume;  //背景音乐
        public float voiceVolume; //音效
        public float textSped; //文字速度

        public int[] achievements; //CG
    }

    
    
    public static class GameSetting
    {
        public static float mscVolume;  //背景音乐
        public static float voiceVolume; //音效
        public static float textSped; //文字速度

        public static int[] achievements; //CG
    }
}

